package com.uzhie.web.controller.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uzhie.exception.UserException;
import com.uzhie.exception.VehicleException;
import com.uzhie.web.anno.Auth;
import com.uzhie.web.bean.PageBean;
import com.uzhie.web.bean.PayloadBean;
import com.uzhie.web.controller.http.comm.CommController;
import com.uzhie.web.module.GpsRecord;
import com.uzhie.web.module.Vehicle;
import com.uzhie.web.service.GpsService;
import com.uzhie.web.service.VehicleService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Zhangdequan on 2018/7/18.
 */
@RestController
@RequestMapping(value = "vehicle")
public class VehicleController extends CommController {

    private Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private VehicleService vehicleService;
    @Autowired
    private GpsService gpsService;
    /**
     * 根据搜索条件检索各个车辆信息
     *
     * @param body
     * @param page
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Auth
    @RequestMapping(value = "list")
    public PayloadBean selectVehicleByCondition(@RequestBody(required = false) JSONObject body, @RequestBody(required = false) PageBean page) {

        List<Vehicle> list = null;
        try {
            list = vehicleService.selectVehicleByCondition(
                    body.getString("departName"),
                    body.getString("name"),
                    body.getString("type"),
                    body.getString("mfrsName"),
                    body.getString("number"),
                    body.getString("simNumber"),
                    page
            );
            return buildResponseWithPage(list, page);
        } catch (IllegalArgumentException e) {
            return buildResponseWithPage(null, page, e.getMessage());
        }
    }

    /**
     * 查询所有可以允许被绑定的
     *
     * @param body userId 当前登录用户的ID
     *             departName 部门名称
     *             type 车辆类型
     *             vehicleNumber 车牌号
     * @param page
     * @return
     */
    @Auth
    @RequestMapping(value = "selectVehicleToBind")
    public PayloadBean selectVehicleToBind(@RequestBody(required = false) JSONObject body, @RequestBody(required = false) PageBean page) {

        List<Vehicle> list = null;
        try {
            list = vehicleService.selectVehicleToBind(body.getString("departId"), body.getString("type"), body.getString("vehicleNumber"), page.getRowBounds());
            return buildResponseWithPage(list, page);
        } catch (IllegalArgumentException | UserException e) {
            return buildResponseWithPage(null, page, e.getMessage());
        }
    }

    /**
     * 新增车辆
     *
     * @param body
     * @return
     */
    @Auth
    @RequestMapping(value = "addVehicle")
    public PayloadBean addVehicle(@RequestBody(required = false) JSONObject body) {
        try {
            vehicleService.addVehicle(JSON.toJavaObject(body, Vehicle.class));
            return buildResponse(null);
        } catch (VehicleException e) {
            return buildResponse(null, e.getMessage());
        }
    }


    /**
     * 根据车辆id,获取车辆详情
     *
     * @param body
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Auth
    @RequestMapping(value = "selectCatalogById")
    public PayloadBean selectVehicleById(@RequestBody(required = false) JSONObject body) {

        try {
            Vehicle vehicle = vehicleService.selectVehicleById(body.getString("vehicleId"));
            return buildResponse(vehicle);
        } catch (IllegalArgumentException e) {
            return buildResponse(null, e.getMessage());
        }
    }


    /**
     * 编辑车辆信息
     *
     * @param body
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Auth()
    @RequestMapping(value = "updateVehicle")
    public PayloadBean updateVehicle(@RequestBody(required = false) JSONObject body) {
        try {
            vehicleService.updateVehicle(JSON.toJavaObject(body, Vehicle.class));
            return buildResponse(null);
        } catch (VehicleException e) {
            return buildResponse(null, e.getMessage());
        }
    }

    /**
     * 根据角色id,批量修改车辆信息数据库状态
     *
     * @param body
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */

    @RequestMapping(value = "updateStatus")
    public PayloadBean updateVehicleStatusById(@RequestBody(required = false) JSONObject body) {
        try {
            int status = body.getInteger("status");
            List<Vehicle> vehicles = JSONArray.parseArray(body.getString("catalogIds"), Vehicle.class);
            vehicleService.updateVehicleStatusById(vehicles, status);
            return buildResponse(null);
        } catch (VehicleException e) {
            return buildResponse(null, e.getMessage());
        }
    }

    /**
     * 车辆日轨迹数据
     * @param body
     * @return
     */

    @Auth
    @RequestMapping(value = "dayTrajectory")
    public PayloadBean queryDayTrajectory(@RequestBody(required = false) JSONObject body) {
        try {
            List<GpsRecord> result = (List<GpsRecord>) gpsService.queryDayTrajectoryByVehicleNumber
                    (body.getString("vehicleNumber"), body.getString("day"));
            return buildResponse(result);
        } catch (IllegalArgumentException e) {
            return buildResponse(null, e.getMessage());
        }
    }


}
